package com.kongfuzi.student.ui.usercenter;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.google.gson.Gson;
import com.kongfuzi.student.R;
import com.kongfuzi.student.bean.MessageEvent;
import com.kongfuzi.student.bean.User;
import com.kongfuzi.student.support.utils.MD5Util;
import com.kongfuzi.student.support.utils.ToastUtil;
import com.kongfuzi.student.app.GasApplication;
import com.kongfuzi.student.support.utils.ArgsConstants;
import com.kongfuzi.student.support.utils.LogUtil;
import com.kongfuzi.student.support.utils.RequestUtil;
import com.kongfuzi.student.support.utils.UrlConstants;
import com.kongfuzi.student.support.volley.Response;
import com.kongfuzi.student.ui.base.CustomerActionBarActivity;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;

public class SetPwdActivity extends CustomerActionBarActivity implements View.OnClickListener {

    @Bind(R.id.et_pwd1)
    EditText mEt_pwd1;
    @Bind(R.id.et_pwd2)
    EditText mEt_pwd2;
    @Bind(R.id.btn_commit)
    Button mBtn_commit;
    private int mType = 1;

    public static Intent newIntent() {
        Intent intent = new Intent(GasApplication.getInstance(), SetPwdActivity.class);
        return intent;
    }

    public static Intent startForgetInstance() {
        Intent intent = newIntent();
        intent.putExtra(ArgsConstants.TITLE,"重置密码");
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_pwd);
        ButterKnife.bind(this);
        String title = getIntent().getStringExtra(ArgsConstants.TITLE);
        if(!TextUtils.isEmpty(title)) {
            mType = 2;
            setCustomerTitle(title);
        }
        initEvents();
    }

    private void initEvents() {
        mBtn_commit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_commit:
                String pwd1 = mEt_pwd1.getText().toString().trim();
                String pwd2 = mEt_pwd2.getText().toString().trim();
                if (TextUtils.isEmpty(pwd1)) {
                    ToastUtil.showToast(this, "请输入密码");
                    return;
                }
                if (TextUtils.isEmpty(pwd2)) {
                    ToastUtil.showToast(this, "请确认密码");
                    return;
                }
                if (pwd1.length() < 6) {
                    ToastUtil.showToast(this, "密码最少6位");
                    return;
                }
                if (!pwd1.equals(pwd2)) {
                    ToastUtil.showToast(this, "两次密码不一致");
                    return;
                }
                String requestUrl = "";
                if(mType == 1) {
                    requestUrl = UrlConstants.REGISTER + "&mobile=" + GasApplication
                            .getMobileNubmer() + "&code=" + GasApplication.getCaptcha() + "&pwd=" +
                            MD5Util.encode(pwd1) + "&repwd=" + MD5Util.encode(pwd2);
                } else if (mType == 2) {
                    requestUrl = UrlConstants.FORGET_PWD + "&mobile=" + GasApplication
                            .getMobileNubmer() + "&code=" + GasApplication.getCaptcha() + "&pwd=" +
                            MD5Util.encode(pwd1) + "&repwd=" + MD5Util.encode(pwd2);
                }
                RequestUtil.requestGet(requestUrl, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        if(response!=null && response.optBoolean("success")) {
                            if(mType == 1) {
                                Gson gson = new Gson();
                                User user = gson.fromJson(response.optJSONObject("data").toString(),
                                        User.class);
                                LogUtil.showInfoLog(mTag, user.toString());
                                GasApplication.saveUser(user);
                                EventBus.getDefault().post(new MessageEvent(MessageEvent.MessageType.REFRESH_MENU_CONTENT));
                                ToastUtil.showToast(SetPwdActivity.this, "注册成功");
                                finish();
                            } else if (mType == 2) {
                                ToastUtil.showToast(SetPwdActivity.this, "新密码已生效");
                                finish();
                            }
                        }
                    }
                },null,mTag);
                break;
        }
    }
}
